﻿#include "mainwindow.h"
#include <QVBoxLayout>  // 用于布局
#include <QHBoxLayout>  // 水平布局
#include <QValueAxis>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    setupChart();

    setupUI();

    resize(1200,1000);
}

MainWindow::~MainWindow()
{
}

void MainWindow::showBarChart()
{
    // 清空之前的图表数据
    clearChart();
    chart->setTitle("柱状图");

    // 创建柱状图数据集
    QBarSet *set0 = new QBarSet("产品 A");
    QBarSet *set1 = new QBarSet("产品 B");
    QBarSet *set2 = new QBarSet("产品 C");

    // 设置每个数据集的值
    *set0 << 1 << 2 << 3 << 4 << 5;
    *set1 << 5 << 0 << 0 << 4 << 3;
    *set2 << 3 << 5 << 8 << 13 << 8;

    // 将数据集加入 QBarSeries（柱状图序列）
    QBarSeries *series = new QBarSeries();
    series->append(set0);
    series->append(set1);
    series->append(set2);

    // 将系列添加到图表
    chart->addSeries(series);

    // 设置X轴类别（假设这是时间段或月份等类别）
    QStringList categories;
    categories << "一月" << "二月" << "三月" << "四月" << "五月";

    QBarCategoryAxis *axisX = new QBarCategoryAxis();
    axisX->append(categories);
    chart->addAxis(axisX, Qt::AlignBottom);
    series->attachAxis(axisX);

    // 创建 Y 轴并设置范围
    QValueAxis *axisY = new QValueAxis();
    axisY->setRange(0, 15);
    chart->addAxis(axisY, Qt::AlignLeft);
    series->attachAxis(axisY);

    chart->legend()->setVisible(true);
    chart->legend()->setAlignment(Qt::AlignBottom);
}

void MainWindow::showPieChart()
{
    // 清空之前的图表数据
    clearChart();
    chart->setTitle("饼状图");

    // 创建饼状图序列
    QPieSeries *series = new QPieSeries();
    series->append("产品 A", 10);
    series->append("产品 B", 20);
    series->append("产品 C", 30);
    series->append("产品 D", 40);

    // 设置某一部分突出显示
    QPieSlice *slice = series->slices().at(1);  // 突出显示产品 B
    slice->setExploded();
    slice->setLabelVisible();
    slice->setPen(QPen(Qt::darkGreen, 2));
    slice->setBrush(Qt::green);

    // 将系列添加到图表
    chart->addSeries(series);
    chart->legend()->setVisible(true);
    chart->legend()->setAlignment(Qt::AlignBottom);
}

void MainWindow::setupChart()
{
    chart = new QChart();
    chart->setTitle("图表演示");

    // 初始状态图表为空
    chartView = new QChartView(chart);
    chartView->setRenderHint(QPainter::Antialiasing);  // 启用抗锯齿
}

void MainWindow::setupUI()
{
    // 创建按钮
    barChartButton = new QPushButton("显示柱状图");
    pieChartButton = new QPushButton("显示饼状图");

    // 连接按钮的点击信号到槽函数
    connect(barChartButton, &QPushButton::clicked, this, &MainWindow::showBarChart);
    connect(pieChartButton, &QPushButton::clicked, this, &MainWindow::showPieChart);

    // 创建布局，按钮放在水平布局中
    QHBoxLayout *buttonLayout = new QHBoxLayout;
    buttonLayout->addWidget(barChartButton);
    buttonLayout->addWidget(pieChartButton);

    // 创建一个垂直布局，把按钮和图表视图放在一起
    QVBoxLayout *mainLayout = new QVBoxLayout;
    mainLayout->addLayout(buttonLayout);
    mainLayout->addWidget(chartView);

    // 创建一个 QWidget 作为中央窗口
    QWidget *centralWidget = new QWidget;
    centralWidget->setLayout(mainLayout);

    // 设置中央部件
    setCentralWidget(centralWidget);
}

void MainWindow::clearChart()
{
    // 清空所有系列
    chart->removeAllSeries();

    // 清空所有坐标轴
    QList<QAbstractAxis *> axes = chart->axes();
    for (QAbstractAxis *axis : axes) {
        chart->removeAxis(axis);
    }

    // 重置图表标题
    chart->setTitle("");
}

